/**
 * @file sigUsr.c
 * @author liangwenhao (1943080020@qq.com)
 * @brief 
 * @version 0.1
 * @date 2021-07-28
 * 
 * @copyright Copyright (c) 2021
 * 
 * @authorGitee  https://gitee.com/liangwenhao
 * @authorGithub https://github.com/WHaoL
 * @authorBlogs https://blog.csdn.net/liangwenhao1108
 *  
 * @Code Description: 	P257-258
 * 						捕捉用户自定义信号
 *  
 */

#include "apue.h"

static void sig_usr(int); /* one handler for both signals */

int main()
{
	if (signal(SIGUSR1, sig_usr) == SIG_ERR)
		err_sys("can't catch SIGUSR1");
	if (signal(SIGUSR2, sig_usr) == SIG_ERR)
		err_sys("can't catch SIGUSR2");

	if (signal(SIGINT, sig_usr) == SIG_ERR) //捕捉Ctrl+c
		err_sys("can't catch SIGINT");

	for (;;)
		pause();

	printf("success\n");
	exit(0);
}

static void sig_usr(int signo) /* argument is signal number */
{
	if (signo == SIGUSR1)
		printf("received SIGUSR1\n");
	else if (signo == SIGUSR2)
		printf("received SIGUSR2\n");

	else if (signo == SIGINT) //捕捉Ctrl+c
		printf("received SIGINT\n");

	else
		err_dump("received signal %d\n", signo);
}

/*

lwh@lwh-HP-Notebook:~/ProjsGit/apue/APUE_my/10_Signals$ ./sigUsr &
[1] 22093
lwh@lwh-HP-Notebook:~/ProjsGit/apue/APUE_my/10_Signals$ kill -USR1 22093
lwh@lwh-HP-Notebook:~/ProjsGit/apue/APUE_my/10_Signals$ received SIGUSR1
kill -USR2 22093
received SIGUSR2
lwh@lwh-HP-Notebook:~/ProjsGit/apue/APUE_my/10_Signals$ kill -USR1 22093
received SIGUSR1
lwh@lwh-HP-Notebook:~/ProjsGit/apue/APUE_my/10_Signals$ kill -USR2 22093
received SIGUSR2
lwh@lwh-HP-Notebook:~/ProjsGit/apue/APUE_my/10_Signals$ kill -USR1 22093
received SIGUSR1
lwh@lwh-HP-Notebook:~/ProjsGit/apue/APUE_my/10_Signals$ kill -USR2 22093
received SIGUSR2

lwh@lwh-HP-Notebook:~/ProjsGit/apue/APUE_my/10_Signals$ kill -9 22093    # 或 kill 22093

*/